#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""  
@Project : wupeiqi_async
@File : 11-单个MySQL连接.py
@Author : Royce
@Time : 2025/7/13 15:48  
@脚本说明 : 
"""
import asyncio
import aiomysql


async def execute(sql_sentence, host, user='root', password='666888', db='mysql', port=3306):
    # 网络IO操作：连接MySQL
    conn = await aiomysql.connect(host=host, port=port, user=user, password=password, db=db)

    # 网络IO操作：创建CURSOR
    cur = await conn.cursor()

    # 网络IO操作：执行SQL
    await cur.execute(sql_sentence)

    # 网络IO操作：获取SQL结果
    result = await cur.fetchall()
    print(f'SQL语句运行结果:{result}')

    # 网络IO操作：关闭链接
    await cur.close()
    conn.close()


if __name__ == '__main__':
    host = '192.168.88.66'
    sql_sentence = "SELECT Host,User FROM user"
    asyncio.run(execute(host=host, sql_sentence=sql_sentence))
